package com.feishu.core;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.feishu.base.dto.entity.ExecutionTypeDO;
import com.feishu.base.service.AddAttributesDataToInService;
import com.feishu.base.type.AttributesInterfaceType;
import com.feishu.service.FeiShuCommonService;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@AttributesInterfaceType(typeValue = "feishuProject",classificationValue="ordinaryMessage")
public class DefaultProcessInformationAttrbutesServiceImpl implements AddAttributesDataToInService {
    @Override
    public void addAttributesToPartDetailsJson(ExecutionTypeDO executionTypeDO, JSONObject partDetailsJson, Object partDetails, Object parameter) {
        FeiShuCommonService feiShuCommonService = SpringUtil.getBean(FeiShuCommonService.class);
        Map<String,Object> map=new HashMap<>();
        map.put("title","飞书应用配置");
        List<Map<String,Object>> list=feiShuCommonService.selectFiguration(map);
        String hostIp="http://10.168.111.204:7006";
        if (list.size() > 0) {
            hostIp=(String) list.get(0).get("acctid");
        }
        Map<String,Object> threadMap= (Map<String, Object>) parameter;

        String audit= (String) threadMap.get("audit");
        String taskId= (String) threadMap.get("taskId");
        String deploymentId= (String) threadMap.get("deploymentId");

        JSONObject jsonObject=new JSONObject();
        for(Map.Entry<String,Object> entity: threadMap.entrySet()){
            jsonObject.put(entity.getKey(),entity.getValue());
        }

        List<Map<String,Object>> groupList=feiShuCommonService.getFeiShuGroupId(deploymentId);
        if(groupList.size()>0){
            jsonObject.put("groupId",groupList.get(0).get("id"));
            jsonObject.put("groupName",groupList.get(0).get("name"));
        }


        if("飞书同步三方审批".equals(executionTypeDO.getLabel())){
            jsonObject.put("approval_code",threadMap.get("approval_code"));
            String workFlowContainerId= (String) threadMap.get("workFlowContainerId");
            if("物料选型".equals(audit)){
                String approval_code= (String) threadMap.get("approval_code");

                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append(hostIp+"/#/homePage/materials?taskId=");
                stringBuilder.append(taskId);
                stringBuilder.append("&approval_code=");
                stringBuilder.append(approval_code);
                jsonObject.put("url",stringBuilder.toString()+"&initiate=1");
                jsonObject.put("initiateUrl",stringBuilder.toString()+"&initiate=0&workFlowContainerId="+workFlowContainerId);
            }
            else if("项目号".equals(audit)){
                String approval_code= (String) threadMap.get("approval_code");
                String refObjectOid= (String) threadMap.get("refObjectOid");

                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append(hostIp+"/#/homePage/project?taskId=");
                stringBuilder.append(taskId);
                stringBuilder.append("&approval_code=");
                stringBuilder.append(approval_code);
                stringBuilder.append("&refObjectOid=");
                stringBuilder.append(refObjectOid);
                jsonObject.put("url",stringBuilder.toString()+"&initiate=1");
                jsonObject.put("initiateUrl",stringBuilder.toString()+"&initiate=0&workFlowContainerId="+workFlowContainerId);
            }
            else if("样机变更".equals(audit)){
                String approval_code= (String) threadMap.get("approval_code");
                String refObjectOid= (String) threadMap.get("refObjectOid");

                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append(hostIp+"/#/homePage/prototype?taskId=");
                stringBuilder.append(taskId);
                stringBuilder.append("&approval_code=");
                stringBuilder.append(approval_code);
                stringBuilder.append("&refObjectOid=");
                stringBuilder.append(refObjectOid);
                jsonObject.put("url",stringBuilder.toString()+"&initiate=1");
                jsonObject.put("initiateUrl",stringBuilder.toString()+"&initiate=0&workFlowContainerId="+workFlowContainerId);
            }
            else if("小批量量产".equals(audit)){
                String approval_code= (String) threadMap.get("approval_code");
                String refObjectOid= (String) threadMap.get("refObjectOid");

                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append(hostIp+"/#/homePage/smallBatchQuantity?taskId=");
                stringBuilder.append(taskId);
                stringBuilder.append("&approval_code=");
                stringBuilder.append(approval_code);
                stringBuilder.append("&refObjectOid=");
                stringBuilder.append(refObjectOid);
                jsonObject.put("url",stringBuilder.toString()+"&initiate=1");
                jsonObject.put("initiateUrl",stringBuilder.toString()+"&initiate=0&workFlowContainerId="+workFlowContainerId);
            }
            else if("设计变更".equals(audit)){
                String approval_code= (String) threadMap.get("approval_code");
                String refObjectOid= (String) threadMap.get("refObjectOid");

                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append(hostIp+"/#/homePage/designChange?taskId=");
                stringBuilder.append(taskId);
                stringBuilder.append("&approval_code=");
                stringBuilder.append(approval_code);
                stringBuilder.append("&refObjectOid=");
                stringBuilder.append(refObjectOid);
                jsonObject.put("url",stringBuilder.toString()+"&initiate=1");
                jsonObject.put("initiateUrl",stringBuilder.toString()+"&initiate=0&workFlowContainerId="+workFlowContainerId);
            }
            else if("小批量变更".equals(audit)){
                String approval_code= (String) threadMap.get("approval_code");
                String refObjectOid= (String) threadMap.get("refObjectOid");

                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append(hostIp+"/#/homePage/smallBatchChange?taskId=");
                stringBuilder.append(taskId);
                stringBuilder.append("&approval_code=");
                stringBuilder.append(approval_code);
                stringBuilder.append("&refObjectOid=");
                stringBuilder.append(refObjectOid);
                jsonObject.put("url",stringBuilder.toString()+"&initiate=1");
                jsonObject.put("initiateUrl",stringBuilder.toString()+"&initiate=0&workFlowContainerId="+workFlowContainerId);
            }
        }
        if("飞书审核BOT消息".equals(executionTypeDO.getLabel())){
            jsonObject.put("approval_code",threadMap.get("approval_code"));
        }



        partDetailsJson.putAll(jsonObject);
    }
}
